package com.sk.bilibili.model.activity;

import com.sk.api.annotations.Autowired;
import com.sk.bilibili.bean.Danmu;
import com.sk.bilibili.bean.Them;
import com.sk.bilibili.callBack.RequestCallBack;
import com.sk.bilibili.mapper.DanmuMapper;
import com.sk.bilibili.mapper.HistoryMapper;
import com.sk.bilibili.mapper.ThemMapper;
import com.sk.bilibili.mapper.VideoSetMapper;
import com.sk.bilibili.model.BaseModel;
import com.sk.bilibili.other.JsonMap;
import com.sk.bilibili.utils.DanmuUtils;
import com.sk.bilibili.utils.DataUtils;
import com.sk.bilibili.utils.RequestUtils;
import com.sk.bilibili.view.activity.ThemActivity;
import com.sk.bilibili.view.activity.VideoSetActivity;

public class ThemModel extends BaseModel<ThemActivity> {

    @Autowired
    ThemMapper themMapper;
    @Autowired
    DanmuMapper danmuMapper;
    @Autowired
    HistoryMapper historyMapper;

    public void loadDanmu(Integer videoSetId) {
        RequestUtils.process(danmuMapper.getAll(videoSetId), danmus ->
                view.loadDanmuSuccess(danmus));
    }

    public Danmu sendDanmu(String content, Integer videoSetId, Float time) {
        Danmu danmu = new Danmu();
        danmu.setColor(DanmuUtils.getRandomColor());
        danmu.setVideoId(videoSetId);
        danmu.setContent(content);
        danmu.setTime(time);
        danmu.setType(DanmuUtils.getRandomType());
        danmu.setMemberId(DataUtils.getMemberId());
        RequestUtils.process(danmuMapper.add(danmu), unused -> view.sendDanmuSuccess());
        return danmu;
    }

    public void loadThem(Integer themId) {
        RequestUtils.process(themMapper.getThem(themId), them -> view.loadThemSuccess(them));
    }

    public void addHistory(Them them) {
        JsonMap map = new JsonMap();
        map.put("type",1);
        map.put("targetId",them.getId());
        map.put("title",them.getExtra().getVideoSet().getTitle());
        RequestUtils.process(historyMapper.add(map));
    }

}